package com.anolesoft.yr.mbus.base.controller;

import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.base.dto.BaseContactDTO;
import com.anolesoft.yr.mbus.base.service.BaseContactService;
import com.anolesoft.yr.mbus.base.vo.BaseContactVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/9 15:15
 */
@RestController
public class BaseContactController {
    @Autowired
    private BaseContactService baseContactService;

    @PostMapping(value = "/api/base/contact/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(BaseContactVO baseContactVO) {
        BaseContactDTO baseContactDTO = vo2dto(baseContactVO, true);
        //return new CommonResult<>(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, baseContactService.create(baseContactDTO));
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, baseContactService.create(baseContactDTO));
    }

    private BaseContactDTO vo2dto(BaseContactVO baseContactVO, boolean newId) {
        return BaseContactDTO.builder()
                .postcode(baseContactVO.getPostcode())
                .contactNumber(baseContactVO.getContactNumber())
                .crmAddress(baseContactVO.getCrmAddress())
                .contactPerson(baseContactVO.getContactPerson())
                .targetUid(baseContactVO.getTargetUid())
                .type(baseContactVO.getType())
                .remark(baseContactVO.getRemark())
                .objuid(newId ? CommonUtils.fastUUID() : baseContactVO.getObjuid())
                .modifyUserUid(SecurityUtils.getUserId())
                .factoryCode(baseContactVO.getFactoryCode())
                .createUserUid(SecurityUtils.getUserId())
                .build();
    }

    @PutMapping(value = "/api/base/contact/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(BaseContactVO baseContactVO) {
        BaseContactDTO baseContactDTO = vo2dto(baseContactVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.MODIFY_SUCCESS_MESSAGE, baseContactService.modify(baseContactDTO));
    }

    @DeleteMapping(value = "/api/base/contact/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, baseContactService.remove(id));
    }
    @DeleteMapping(value = "/api/base/contact/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@RequestParam("ids[]") List<String> ids) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, baseContactService.removeBatch(ids));
    }

    @GetMapping(value = "/api/base/contact/get/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<BaseContactDTO> baseContactDTO = this.baseContactService.selectOneById(id);
        if (baseContactDTO.isPresent()) {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, dto2vo(baseContactDTO.get()));
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }

    }

    @GetMapping(value = "/api/base/contact/list/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectAll() {
        List<BaseContactVO> vos = baseContactService.selectAll().stream().map(t -> dto2vo(t)).collect(Collectors.toList());
        PageInfo<List<BaseContactVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/base/contact/list/all/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectAll(
            @RequestParam("rows") Integer limit,
            @RequestParam("page") Integer curr) {
//        List<BaseContactVO> vos = baseContactService.selectAll(limit, curr).stream().map(t -> dto2vo(t)).collect(Collectors.toList());
//        PageInfo<List<BaseContactVO>> pageInfo = new PageInfo<>(limit, vos.size(), vos);
//        return new CommonResult<>(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
        PageData<List<BaseContactDTO>> pageData = baseContactService.selectAll(limit, curr);
        List<BaseContactVO> vos = pageData.getItems().stream().map(t -> dto2vo(t)).collect(Collectors.toList());
        PageInfo<List<BaseContactVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/base/contact/list/condition", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectList(BaseContactVO baseCompanyVO) {
        BaseContactDTO baseContactDTO = vo2dto(baseCompanyVO, false);
        List<BaseContactVO> vos = this.baseContactService.selectList(baseContactDTO).stream().map(t -> dto2vo(t)).collect(Collectors.toList());
        PageInfo<List<BaseContactVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/base/contact/list/condition/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectListPage(BaseContactVO baseContactVO) {
        BaseContactDTO baseContactDTO = vo2dto(baseContactVO, false);
        Integer limit = baseContactVO.getRows();
        Integer curr = baseContactVO.getPage();
//        List<BaseContactVO> vos = this.baseContactService.selectList(baseContactDTO, limit, curr).stream().map(t -> dto2vo(t)).collect(Collectors.toList());
//        PageInfo<List<BaseContactVO>> pageInfo = new PageInfo<>(limit, vos.size(), vos);
//        return new CommonResult<>(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);

        PageData<List<BaseContactDTO>> pageData = baseContactService.selectList(baseContactDTO, limit, curr);
        List<BaseContactVO> vos = pageData.getItems().stream().map(t -> dto2vo(t)).collect(Collectors.toList());
        PageInfo<List<BaseContactVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    private BaseContactVO dto2vo(BaseContactDTO baseContactDTO) {
        return BaseContactVO.builder()
                .contactNumber(baseContactDTO.getContactNumber())
                .contactPerson(baseContactDTO.getContactPerson())
                .createTime(baseContactDTO.getCreateTime())
                .createUserUid(baseContactDTO.getCreateUserUid())
                .crmAddress(baseContactDTO.getCrmAddress())
                .deleteFlag(baseContactDTO.getDeleteFlag())
                .factoryCode(baseContactDTO.getFactoryCode())
                .modifyTime(baseContactDTO.getModifyTime())
                .modifyUserUid(baseContactDTO.getModifyUserUid())
                .objuid(baseContactDTO.getObjuid())
                .postcode(baseContactDTO.getPostcode())
                .remark(baseContactDTO.getRemark())
                .targetUid(baseContactDTO.getTargetUid())
                .type(baseContactDTO.getType())
                .build();
    }
}